from datetime import datetime

from flask import request

from air_web.web_flask.bll.user_predict_bll import UserPredictBll
from air_web.web_flask.common.constants import (
    predict_columns_chinese_dict_cons,
)
from air_web.web_flask.common.validator import (
    T_CHINESE,
    T_DATE,
    T_INT,
    T_LIST,
    T_STR,
    Fields,
    with_validator,
)
from air_web.web_flask.views.base_page import BasePage


class UserPredictPageValidationFields:
    input_fields = [
        Fields("cons_name", T_STR, must=False),
        Fields("cons_no", T_STR, must=False),
        Fields("start_date", T_DATE, must=False),
        Fields("download", T_STR, must=False, default="false"),
    ]

    cons_fields = [
        Fields("cons_name", T_STR, must=False),
    ]


class UserPredictPage(BasePage):
    """总览页面视图函数类"""

    @classmethod
    @with_validator(request, UserPredictPageValidationFields.input_fields)
    def user_air_predict(cls):
        validation_data = request.validation_data
        is_download, validation_data = BasePage.get_is_download(
            validation_data
        )
        result_data, statistic_data = UserPredictBll().get_all_result(
            validation_data, is_download
        )
        if is_download:
            columns = result_data.columns.to_list()
            two_column_list = list(
                set(columns)
                - set(list(predict_columns_chinese_dict_cons.keys()))
            )
            two_column_list.sort()
            predict_columns_chinese_dict_cons.update(
                {col: col for col in two_column_list}
            )
            return cls.get_return_excel_file_from_df(
                result_data,
                predict_columns_chinese_dict_cons,
                sheet_name="用户负荷预测数据",
                file_name="air_predict_table",
                round_two_decimal_column=two_column_list,
            )
        result = [
            {
                "data": result_data,
                "rate": [statistic_data],
            }
        ]
        return cls.return_common_func(result)

    @classmethod
    @with_validator(request, UserPredictPageValidationFields.cons_fields)
    def user_cons_search(cls):
        args = request.validation_data
        result = UserPredictBll().get_user_cons(args)
        return cls.return_common_func(result)
